<?php
    class Subject extends MY_controller{

        function __construct(){
            parent::__construct();
        }

        function index($title = 0, $tk_category_id = 0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $tk_category_id = !empty($post_data['tk_category_id']) ? $post_data['tk_category_id'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('title' => $title, "tk_category_id" => $tk_category_id);
            $this->load->model("Subjects");
            $result = $this->Subjects->get_subject_list($limit, $this->offset, $conditions);
            $this->load->library('pagination');
            $page_config['base_url'] = '/subject/index/' . $title . '/' . $tk_category_id;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 5;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();
            $this->load->model("Tk_categorys");
            $category_list = $this->Tk_categorys->get_top_categorys();
            $this->smarty->assign("category_list", $category_list);
            $this->smarty->assign("action", '科目列表');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("subject_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->display("subject/list.html");
        }

        function school_tiku($title = 0, $school_id = 0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $school_id = !empty($post_data['school_id']) ? $post_data['school_id'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('title' => $title, "school_id" => $school_id);
            $this->load->model("Subjects");
            $result = $this->Subjects->get_subject_list($limit, $this->offset, $conditions);
            $this->load->library('pagination');
            $page_config['base_url'] = '/subject/school_tiku/' . $title . '/' . $school_id;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 5;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();
            $this->load->model("Schools");
            $school_list = $this->Schools->get_school_list();
            $this->smarty->assign("school_list", $school_list);
            $this->smarty->assign("action", '科目列表');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("subject_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->assign("type", 7);
            $school_info['id'] = $school_id;
            $this->smarty->assign("school_info", $school_info);
            $this->smarty->display("subject/school_tiku.html");
        }

        function add(){
            $post_data = $this->input->post();
            if($post_data){
                if(isset($post_data['file'])) unset($post_data['file']);
                if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
                $post_data['create_on'] = time();
                $this->db->insert("b_subject", $post_data);
                $insert_id = $this->db->insert_id();
                if($insert_id > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                $this->load->model("Tk_categorys");
                $category_list = $this->Tk_categorys->get_top_categorys();
                $this->smarty->assign("category_list", $category_list);
                $this->smarty->assign("action", '添加题库');
                $this->smarty->assign("post_url", '/subject/add');
                $this->smarty->display("subject/save.html");
            }
        }

        function edit($id = 0){
            $post_data = $this->input->post();
            if($post_data){
                if(isset($post_data['file'])) unset($post_data['file']);
                if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
                $this->db->where('id', $post_data['id']);
                $this->db->update('b_subject', $post_data);
                if($this->db->affected_rows() > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                if(intval($id) > 0){
                    $subject_info = get_field_by_where("b_subject", "*", "id='{$id}'");
                    if(!$subject_info){
                        show_message($this->jump_url, "数据不存在");exit;
                    }
                    $this->load->model("Tk_categorys");
                    $category_list = $this->Tk_categorys->get_top_categorys();
                    $this->smarty->assign("subject_info", $subject_info);
                    $this->smarty->assign("category_list", $category_list);
                    $this->smarty->assign("post_url", '/subject/edit');
                    $this->smarty->assign("action", '编辑科目');
                    $this->smarty->display("subject/save.html");
                }else{
                    show_message($this->jump_url, "非法操作");
                }
            }
        }

        function delete($ids = 0){
            if($_POST){
                $ids = $this->input->post('ids');
            }else{
                $ids = (array) $ids;
            }
            delete_data($ids, "b_subject");
            if($this->db->affected_rows() > 0){
                show_message($this->jump_url, "删除成功");
            }else{
                show_message($this->jump_url, "删除失败，请重试！");
            }
        }

        function index_chapter($subject_id = 0, $parent_id = 0, $title = 0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
                $parent_id = !empty($post_data['parent_id']) ? $post_data['parent_id'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('subject_id' => $subject_id, 'parent_id' => $parent_id, 'title' => $title);
            $this->load->model("Subjects");
            $result = $this->Subjects->get_chapter_list($limit, $this->offset, $conditions);
            $this->load->library('pagination');
            $page_config['base_url'] = '/subject/index_chapter/' . $subject_id . '/' . $parent_id . '/' . $title;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 6;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();
            $subject_list = $this->Subjects->get_subject_list();
            $this->smarty->assign("subject_list", $subject_list['result']);
            $this->smarty->assign("action", '章节列表');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("chapter_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->display("subject/chapter_list.html");
        }

        function add_chapter(){
            $post_data = $this->input->post();
            if($post_data){
                $post_data['create_on'] = time();
                if(!isset($post_data['is_chapter'])) $post_data['is_chapter'] = 0;
                $this->db->insert("b_subject_chapter", $post_data);
                $insert_id = $this->db->insert_id();
                if($insert_id > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                $this->load->model("Subjects");
                $subject_list = $this->Subjects->get_subject_list();
                $this->smarty->assign("subject_list", $subject_list['result']);
                $this->smarty->assign("action", '添加题库');
                $this->smarty->assign("post_url", '/subject/add_chapter');
                $this->smarty->display("subject/chapter_save.html");
            }
        }

        function edit_chapter($id = 0){
            $post_data = $this->input->post();
            if($post_data){
                if(!isset($post_data['is_chapter'])) $post_data['is_chapter'] = 0;
                $this->db->where('id', $post_data['id']);
                $this->db->update('b_subject_chapter', $post_data);
                if($this->db->affected_rows() > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                if(intval($id) > 0){
                    $chapter_info = get_field_by_where("b_subject_chapter", "*", "id='{$id}'");
                    if(!$chapter_info){
                        show_message($this->jump_url, "数据不存在");exit;
                    }
                    $this->load->model("Subjects");
                    $chapter_list_data = $this->get_chapter_list($chapter_info['subject_id']);
                    
                    $subject_list = $this->Subjects->get_subject_list();
                    $this->smarty->assign("subject_list", $subject_list['result']);
                    $this->smarty->assign("chapter_list_data", $chapter_list_data);
                    $this->smarty->assign("chapter_info", $chapter_info);
                    
                    $this->smarty->assign("post_url", '/subject/edit_chapter');
                    $this->smarty->assign("action", '编辑科目');
                    $this->smarty->display("subject/chapter_save.html");
                }else{
                    show_message($this->jump_url, "非法操作");
                }
            }
        }

        function get_chapter_list($subject_id){
            $this->load->model("Topics");
            $conditions['subject_id'] = $subject_id;
            $chapter_list = $this->Topics->get_chapter($conditions);
            $chapter_list_data = array();
            $code = 0;
            if($chapter_list){
                foreach ($chapter_list as $key => $value) {
                    if($value['parent_id'] == 0){
                        $chapter_list_data[$value['id']] = $value;
                        unset($chapter_list[$key]);
                    }
                }

                foreach ($chapter_list as $key => $value) {
                    if(isset($chapter_list_data[$value['parent_id']])){
                        $chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
                        unset($chapter_list[$key]);
                    }
                }

                if($chapter_list AND $chapter_list_data){
                    foreach ($chapter_list_data as $key => $value) {
                        if($value['sub']){
                            foreach ($value['sub'] as $k => $v) {
                                foreach ($chapter_list as $k1 => $v1) {
                                    if($k == $v1['parent_id']){
                                        $chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return $chapter_list_data;
        }

        function delete_chapter($ids = 0){
            if($_POST){
                $ids = $this->input->post('ids');
            }else{
                $ids = (array) $ids;
            }
            delete_data($ids, "b_subject_chapter");
            if($this->db->affected_rows() > 0){
                show_message($_SERVER['HTTP_REFERER'], "删除成功");
            }else{
                show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
            }
        }

        function update_sort(){
            $sort = $this->input->post('sort');
            if(is_array($sort) && count($sort) > 0){
                foreach($sort as $key=>$val) {
                    $this->db->where('id', $key);
                    $this->db->update('b_subject_chapter', array('sort'=>$val));
                }
            }
            show_message($_SERVER['HTTP_REFERER'], "操作成功");
        }
    }